package com.cate.controller.admin;

import com.cate.base.BasePageVO;
import com.cate.constants.JwtClaimsConstant;
import com.cate.context.BaseContext;
import com.cate.pojo.dto.EmployeeEditPasswordDTO;
import com.cate.pojo.dto.EmployeeInfoDTO;
import com.cate.pojo.dto.EmployeeLoginDTO;
import com.cate.pojo.dto.EmployeePageDTO;
import com.cate.pojo.entity.Employee;
import com.cate.exception.BusinessException;
import com.cate.properties.JwtProperties;
import com.cate.result.RestResponse;
import com.cate.result.SystemCode;
import com.cate.service.EmployeeService;
import com.cate.utils.JasyptUtil;
import com.cate.utils.JwtUtil;
import com.cate.utils.ResultUtil;
import com.cate.pojo.vo.EmployeeLoginVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * @CreateTime: 2023/7/26
 * @Author: <==lhd==>
 * @Description: 员工管理
 */
@RestController
@RequestMapping("/admin/employee")
@Slf4j
@Api(tags = "员工管理")
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;

    @Resource
    private JwtProperties jwtProperties;


    @PostMapping("/login")
    @ApiOperation(value = "员工登录")
    public RestResponse<EmployeeLoginVO> login(@RequestBody EmployeeLoginDTO employeeLoginDTO){
        log.info("用户登录username={}",employeeLoginDTO.getUsername());
        // 获得登录的用户信息
        final Employee employee = employeeService.login(employeeLoginDTO);

        // 判断用户是否存在
        if(employee==null){
            throw new BusinessException(SystemCode.REQUEST_ERROR,"该用户不存在");
        }

        // 判断密码是否正确
        if(!JasyptUtil.checkPassword(employeeLoginDTO.getPassword(),employee.getPassword())){
            throw new BusinessException(SystemCode.AUTH_ERROR);
        }

        // 创建token
        Map<String,Object> tokenMap = new HashMap<>();
        tokenMap.put(JwtClaimsConstant.EMP_ID,employee.getId());
        // 将用户信息存在BaseContext
        BaseContext.setCurrentId(employee.getId());
        String token = JwtUtil.createJWT(jwtProperties.getAdminSecretKey(),jwtProperties.getAdminTtl(),tokenMap);

        EmployeeLoginVO employeeLoginVO = new EmployeeLoginVO();
        employeeLoginVO.setId(employee.getId());
        employeeLoginVO.setName(employee.getName());
        employeeLoginVO.setToken(token);
        employeeLoginVO.setUserName(employee.getUsername());
        return ResultUtil.success(employeeLoginVO,"登录成功");
    }

    @PutMapping("/editPassword")
    @ApiOperation(value = "密码修改")
    public RestResponse editPassword(@RequestBody EmployeeEditPasswordDTO employeeEditPasswordDTO){
        employeeService.editPassword(employeeEditPasswordDTO);
        return ResultUtil.success(null);
    }

    @PostMapping("/status/{status}")
    @ApiOperation(value = "设置用户账号状态")
    public RestResponse setStatus(@PathVariable("status") Integer status,@RequestParam("id") Long id){
        employeeService.setStatus(status,id);
        return ResultUtil.success(null);
    }

    @GetMapping("/page")
    @ApiOperation("分页获得员工列表")
    public RestResponse<Object> queryPage(@Validated EmployeePageDTO employeePageDTO){
        final BasePageVO<Employee> employeeBasePageVO = employeeService.queryPage(employeePageDTO);
        return ResultUtil.success(employeeBasePageVO);
    }

    @PostMapping
    @ApiOperation("添加员工")
    public RestResponse<Object>  save(@RequestBody @Validated EmployeeInfoDTO employeeInfoDTO){
        employeeService.save(employeeInfoDTO);
        return ResultUtil.success(null);
    }

    @GetMapping("/{id}")
    @ApiOperation("根据id查询员工")
    public RestResponse<Employee> getById(@PathVariable("id") Long id){
        return ResultUtil.success(employeeService.getById(id));
    }

    @PutMapping
    @ApiOperation("编辑员工信息")
    public RestResponse<Object> update(@RequestBody @Validated EmployeeInfoDTO employeeInfoDTO){
        employeeService.update(employeeInfoDTO);
        return ResultUtil.success(null);
    }

    @PostMapping("/logout")
    @ApiOperation("退出成功")
    public  RestResponse<Object> logout(){
        BaseContext.removeCurrentId();
        return ResultUtil.success(null,"退出成功");
    }

}
